<?
function imagecreatefrom($image, $mime_type) {
	switch ($mime_type) {
		case 'image/jpeg':
			return imagecreatefromjpeg($image);
			break;
		case 'image/gif':
			$im = imagecreatefromgif($image);
			imagecolortransparent($im, fromhex('FFFFFF', $im));
			return $im;
			break;
		case 'image/png':
			return imagecreatefrompng($image);
			break;			
	}
	
}



/*
 * resizeandpad($source, $width, $height)
 * 
 * resize the image to the longest edge and pad
 *
 */
function resizeandpad($source, $width, $height, $colour='FFFFFF') {
	$sourcex = imagesx($source);
	$sourcey = imagesy($source);
	$xratio = $sourcex / $width;
	$yratio = $sourcey / $height;	
	if ($xratio > $yratio) {
		$sized = resizetowidth($source, $width);
	} else {
		$sized = resizetoheight($source, $height);
	}
	$sizedx = imagesx($sized);
	$sizedy = imagesy($sized);
	$newimage = pad($sized, ceil(($width-$sizedx)/2), ceil(($width-$sizedx)/2), ceil(($height-$sizedy)/2), ceil(($height-$sizedy)/2), $colour);	
	return $newimage;	
}
/*
 * resizeandcrop($source, $width, $height)
 * 
 * resize the image to the shortest edge and then crop
 *
 */
function resizeandcrop($source, $width, $height) {
	$sourcex = imagesx($source);
	$sourcey = imagesy($source);
	$xratio = $sourcex / $width;
	$yratio = $sourcey / $height;	
	if ($xratio < $yratio) {
		$sized = resizetowidth($source, $width);
	} else {
		$sized = resizetoheight($source, $height);
	}
	$sizedx = imagesx($sized);
	$sizedy = imagesy($sized);
	$newimage = crop($sized, ($sizedx-$width)/2, ($sizedx-$width)/2, ($sizedy-$height)/2, ($sizedy-$height)/2);
	return $newimage;
}
/*
 * resizetowidth($source, $width)
 * 
 * calculate the correct height to go with the width
 * and call resize()
 *
 */
function resizetowidth($source, $width) {
	$sourcex = imagesx($source);
	$sourcey = imagesy($source);
	$destx = $width;
	$desty = ceil($sourcey / ($sourcex / $width));
	return(resize($source, $destx, $desty));
}
/*
 * resizetoheight($source, $height)
 * 
 * calculate the correct width to go with the height
 * and call resize()
 *
 */
function resizetoheight($source, $height) {
	$sourcex = imagesx($source);
	$sourcey = imagesy($source);
	$desty = $height;
	$destx = ceil($sourcex / ($sourcey / $height));
	return(resize($source, $destx, $desty));
}
/*
 * resizetomax($source, $max)
 * 
 * calculate which edge is the longest, and call
 * resizetoheight() or resizetowidth() appropriately
 *
 */
function resizetomax($source, $max) {
	$sourcex = imagesx($source);
	$sourcey = imagesy($source);
	if ($sourcex > $sourcey) {
		return resizetowidth($source, $max);
	} else {
		return resizetoheight($source, $max);
	}
}
/*
 * resize($source, $width, $height)
 * 
 * resize the image to the requested dimensions and
 * return the new image.
 *
 */
function resize($source, $width, $height) {
	$sourcex = imagesx($source);
	$sourcey = imagesy($source);
	$newimage = imagecreatetruecolor($width, $height);
	imagecopyresampled($newimage, $source, 0, 0, 0, 0, $width, $height, $sourcex, $sourcey);	
	return $newimage;
}
/*
 * pad($source, $left, $right, $top, $bottom, $colour)
 * 
 * pad the source image the specified amounts and
 * using the specified colour 
 *
 */
function pad($source, $left, $right, $top, $bottom, $colour) {
	$sourcex = imagesx($source);
	$sourcey = imagesy($source);
	$newimage = imagecreatetruecolor($sourcex + $left + $right, $sourcey + $top + $bottom);
	ImageFill($newimage, 0, 0, fromhex($colour, $newimage));
	imagecopy($newimage, $source, $left, $top, 0, 0, $sourcex, $sourcey);		
	return $newimage;	
}
/*
 * crop($source, $left, $right, $top, $bottom)
 *
 * crop the image by the specified amounts
 * 
 */
function crop($source, $left, $right, $top, $bottom) {
	$sourcex = imagesx($source);
	$sourcey = imagesy($source);
	$newimage = imagecreatetruecolor($sourcex - $left - $right, $sourcey - $top - $bottom);
	imagecopy($newimage, $source, 0, 0, $left, $top, $sourcex - $left - $right, $sourcey - $top - $bottom);		
	return $newimage;

	
}

function fromhex($string,&$image) {
	sscanf($string, "%2x%2x%2x", $red, $green, $blue);
	return ImageColorAllocate($image,$red,$green,$blue);
}
